#!/bin/sh
export UNICONF=unix:/tmp/uniconfd

status()                                                                      
{                                                                             
	CODE=$1
	shift
	MESSAGE="$@"
	echo "[$CODE] $MESSAGE"
	uni set /tmp/addons/$MYNAME/status "$CODE"
	uni set /tmp/addons/$MYNAME/status\ message "$MESSAGE"
}

statusize()
{
	PREFIX="$1"
	shift
	STEP=0
	( "$@" 2>&1 || echo FAILED ) | while read x y; do
		if [ "$x" = "--" ]; then
			STEP=$(($STEP + 1))
			status 1 "$PREFIX (#$STEP: $y)"
		elif [ "$x" = "FAILED" ]; then
			status -1 "FAILED ($LAST)"
			return 1
		else
			echo "$x $y"
		fi
		LAST="$x $y"
	done
}

status 1 "Doing stuff..."

for d in *.vip; do
	if [ -e "$d" ]; then
		if [ -e "$d.installed" ]; then
			status 1 "Skipping $d (already installed)."
		else
			status 1 "Installing $d..."
			if statusize "Installing $d" vipinstall "$d"; then
				touch "$d.installed"
				status 1 "Installed $d successfully."
			else
				exit 1
			fi
		fi
	fi
done

exit 0
